package com.aysafety.iotdataplatformclient.hydraulicSupport.services.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.HydraulicSupportModel;
import com.aysafety.iotdataplatformclient.hydraulicSupport.dao.HydraulicSupportDao;
import com.aysafety.iotdataplatformclient.hydraulicSupport.services.HydraulicSupportService;
import com.aysafety.iotdataplatformclient.modelConfigurer.HydraulicSupportModelConfiger;

@Service
public class HydraulicSupportServiceImpl implements HydraulicSupportService {

    @Autowired
    HydraulicSupportModelConfiger hydraulicSupportModelConfiger;

    @Autowired
    HydraulicSupportDao hydraulicSupportDao;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private HydraulicSupportModel transformData(Map<String, Object> temp, Map<String, String> structure) {
        HydraulicSupportModel hydraulicSupportModel = new HydraulicSupportModel();
        for (String key : structure.keySet()) {
            String columnKey = structure.get(key);
            if ("EntId".equals(key)) {
                hydraulicSupportModel.setEntId(hydraulicSupportModelConfiger.getEntId());
            }
            if ("deviceid".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    hydraulicSupportModel.setDeviceId((String) temp.get(columnKey));
                }
            }
            if ("leftpressure".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    hydraulicSupportModel.setLeftPressure(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("rightpressure".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    hydraulicSupportModel.setRightPressure(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("totalstrain".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    hydraulicSupportModel.setTotalStrain(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("maxvalue".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    hydraulicSupportModel.setMaxValue(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("minvalue".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    hydraulicSupportModel.setMinValue(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("datetime".equals(key)) {
                String[] columnTemp = columnKey.split("\\+");
                String dateTimeStr = "";
                for (String value : columnTemp) {
                    if (temp.containsKey(value)) {
                        dateTimeStr += temp.get(value) + " ";
                    }
                }
                dateTimeStr = dateTimeStr.trim();
                hydraulicSupportModel.setDatetime(DateUtils.fomatDateTime(dateTimeStr));
            }
            if ("workingface".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    hydraulicSupportModel.setWorkingFace((String) temp.get(columnKey));
                }
            }
            if ("devicepos".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    hydraulicSupportModel.setDevicePos((String) temp.get(columnKey));
                }
            }

        }
        return hydraulicSupportModel;
    }

    public List<HydraulicSupportModel> readData(Map<String, Object> conditions) throws Exception {
        List<HydraulicSupportModel> readHistroyDataModel = new ArrayList<HydraulicSupportModel>();
        Map<String, String> structure = hydraulicSupportModelConfiger.getDatakey();
        List<Map<String, Object>> readHistroyDatas = hydraulicSupportDao.lastData(conditions);
        for (Map<String, Object> readHistroyData : readHistroyDatas) {
            HydraulicSupportModel hydraulicSupportModel = this.transformData(readHistroyData, structure);
            readHistroyDataModel.add(hydraulicSupportModel);
        }
        return readHistroyDataModel;
    }

    public int readHistroyDataCount(Map<String, Object> conditions) {
        return hydraulicSupportDao.readHistroyDataCount(conditions);
    }

    public List<HydraulicSupportModel> readHistroyData(Map<String, Object> conditions, int start, int prePage) {
        List<HydraulicSupportModel> readHistroyDataModel = new ArrayList<HydraulicSupportModel>();
        Map<String, String> structure = hydraulicSupportModelConfiger.getDatakey();
        List<Map<String, Object>> readHistroyDatas = hydraulicSupportDao.readHistroyData(conditions, start, prePage);
        for (Map<String, Object> readHistroyData : readHistroyDatas) {
            HydraulicSupportModel hydraulicSupportModel = this.transformData(readHistroyData, structure);
            readHistroyDataModel.add(hydraulicSupportModel);
        }
        return readHistroyDataModel;
    }
}
